<?php


namespace App\Utils\Helper;


use App\Enum\AppEnum;
use App\Utils\Traits\ControllerTrait;
use Sonata\AdminBundle\Controller\CRUDController;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\HttpFoundation\Request;

class ControllerHelper extends CRUDController {
    use ControllerTrait;

    protected function getSecurity () {
        return $this->security;
    }

    protected function preEdit (Request $request, $object) {
        return $this->verifyAppMode($request, $object, $this->admin, 'edit');
    }

    protected function preCreate (Request $request, $object) {
        return $this->verifyAppMode($request, $object, $this->admin, 'create');
    }

    protected function preDelete (Request $request, $object) {
        return $this->verifyAppMode($request, $object, $this->admin, 'delete');
    }

    public function batchActionDelete (ProxyQueryInterface $query) {
        $app_mode = $this->container->getParameter('app.mode');
        if ($app_mode == AppEnum::MODE_DEMO) {
            $this->addFlash('sonata_flash_error', 'Demo模式下禁止批量删除数据');
            return $this->redirectToList();
        } else {
            return parent::batchActionDelete($query); // TODO: Change the autogenerated stub
        }
    }
}